<?php	

class match {
	var $TeamOne;
	var $TeamTwo;
	var $TeamOnePoints;
	var $TeamTwoPoints;
	var $Winner;
	var $Week;
	
	function match($TeamOne, $TeamTwo, $Week)
	{
		$this->TeamOne = $TeamOne;
		$this->TeamTwo = $TeamTwo;
		$this->Week = $Week;
		
		$this->TeamOnePoints = -999;	// -999 just in case a bye happens to win!
		$this->TeamTwoPoints = -999;
		if ($this->TeamOne->getMemberName() != "BYE")
		{
			$this->TeamOnePoints = $this->TeamOne->getScoreForWeek($this->Week);
		}
		if ($this->TeamTwo->getMemberName() != "BYE")
		{
			$this->TeamTwoPoints = $this->TeamTwo->getScoreForWeek($this->Week);
		}

		//echo "<BR>".$this->TeamOne->getMemberName()." v ".$this->TeamTwo->getMemberName();
	}
	
	function GetWinner()
	{
		if ($this->TeamOnePoints > $this->TeamTwoPoints)
		{
			return $this->TeamOne;
		}
		else if ($this->TeamOnePoints < $this->TeamTwoPoints)
		{
			return $this->TeamTwo;
		}
		else
		{
			// It's a draw - home team has advantage
			return $this->TeamOne;
		}
	}
}


class round {
	var $Round;
	var $Week;
	var $Matches;
	var $RoundLabel;
	var $cMatch = array();
	
	function round($Round, $numberRounds, $Byes, $Matches)
	{
		$this->Round = $Round;
		$this->Matches = $Matches;
		
		if ($this->Round == $numberRounds)
		{
			$this->RoundLabel = "Final";
		}
		else if ($this->Round == ($numberRounds-1))
		{
			$this->RoundLabel = "Semi Final";
		}
		else if ($this->Round == ($numberRounds-2))
		{
			$this->RoundLabel = "Quarter Finals";
		}
		else
		{
			$this->RoundLabel = "Round".$this->Round;
		}
		
		$cDatabase = new database();
		$connect = $cDatabase->connectToDatabase();
		$activeDatabase = $cDatabase->getActiveDatabase($connect);
		$db = $cDatabase->selectDatabase($connect, $activeDatabase);

		// get weeks for each round
		$selectArray[0] = '*';
		$query = "SELECT * FROM tandbergCup LIMIT 1";
		$result = mysqli_query($connect, $query);

		$row = mysqli_fetch_array($result, MYSQLI_NUM);
		
		$this->Week = $row[$Round+4];
		
		//echo"<BR>Round ".$Round." Week ",$this->Week;
		
		// Get matches
		if ($Round == 1)
		{
			// Get members
			$cMember = array();
			$selectArray[0] = '*';
			$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, NULL, NULL, NULL, NULL);

			$finfo = mysqli_fetch_fields($result);
				
			for ($i = 0; $i < mysqli_num_rows($result); $i++)	// Do each member's team at a time
			{
				$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
				
				// Get all the data for this member and put it in the class
				$cMember[$i] = new member;

				$error |= $cMember[$i]->setMemberName($row['memberName']);
				$error |= $cMember[$i]->setTeamName($row['teamName']);
				$error |= $cMember[$i]->setMoneyPaid($row['moneyPaid']);
				$error |= $cMember[$i]->setActive($row['active']);
				$error |= $cMember[$i]->loadMemberPlayerInfo($row, $connect);
			}
			for ($j = 0; $j <= $Byes; $j++)
			{
				$cMember[$i+$j] = new member;
				$error |= $cMember[$i+$j]->setMemberName("BYE");
			}
			
		
			$selectArray[0] = '*';
			$result = $cDatabase->selectFromDatabase($connect, 'tandbergCup', $selectArray, NULL, NULL, NULL, NULL);
			if($result != 0)
			{
				for ($i = 0; $i < mysqli_num_rows($result); $i++)
				{
					$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
					//echo "<BR>".$row['teamOne']." v ".$row['teamTwo'];
					$this->cMatch[$i] = new match($cMember[$row['teamOne']], $cMember[$row['teamTwo']], $this->Week);
				}
			}
		}
		
		$cDatabase->closeDatabase($connect);
	}
	
	function CreateMatch($TeamOne, $TeamTwo, $Match)
	{
		$this->cMatch[$Match] = new match($TeamOne, $TeamTwo, $this->Week);
	}
	
	function GetMatch($Match)
	{
		return $this->cMatch[$Match];
	}
	
	function GetRoundNum()
	{
		return $this->Round;
	}
}


class cup {
	var $Rounds;
	var $Byes;
	var $cRound = array();
	
	function cup($numMembers)
	{
		if ($this->CheckCupDrawHasHappened() == 0)
			return 0;
		if ($numMembers > 64)
			$this->Rounds=7;
		else if ($numMembers > 32)
			$this->Rounds=6;
		else if ($numMembers > 16)
			$this->Rounds=5;
		else if ($numMembers > 8)
			$this->Rounds=4;
		else if ($numMembers > 4)
			$this->Rounds=3;
		else if ($numMembers > 2)
			$this->Rounds=2;
		else
			$this->Rounds=1;
			
		if ($numMembers > 64)
			$Teams=128;
		else if ($numMembers > 32)
			$Teams=64;
		else if ($numMembers > 16)
			$Teams=32;
		else if ($numMembers > 8)
			$Teams=16;
		else if ($numMembers > 4)
			$Teams=8;
		else if ($numMembers > 2)
			$Teams=4;
		else
			$Teams=2;
			
		$this->Byes = $Teams - $numMembers;
			
		for ($roundNum = 1; $roundNum <= $this->Rounds; $roundNum++)
		{
			$matches = $Teams / pow(2, $roundNum);

			$this->cRound[$roundNum] = new round($roundNum, $this->Rounds, $this->Byes, $matches);
		}
	}
	
	function CheckCupDrawHasHappened()
	{
		$cDatabase = new database();
		$connect = $cDatabase->connectToDatabase();
		$activeDatabase = $cDatabase->getActiveDatabase($connect);
		$db = $cDatabase->selectDatabase($connect, $activeDatabase);

		// get weeks for each round
		$selectArray[0] = '*';
		$query = "SELECT * FROM tandbergCup LIMIT 1";
		$result = mysqli_query($connect, $query);
		if ($result == 0)
		{
			return 0;
		}
		return 1;
	}
	
	function GetRound($Round)
	{
		return $this->cRound[$Round];
	}
	
	function GetNumberRounds()
	{
		return $this->Rounds;
	}
}

?>